package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.web.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/daily")
public class DailyController {

    @Autowired
    private IDailyService dailyService;

    @RequestMapping("/list")
    public String query(@ModelAttribute("qo")DailyQueryObject qo,Model model){
        model.addAttribute("result", dailyService.query(qo));
        return "/daily/list";
    }

    @RequestMapping("/getDailyContentByDailyId")
    @ResponseBody
    public Object getDailyContentByDailyId(Long dailyId){
        return dailyService.getDailyContentByDailyId(dailyId);
    }

    //新增或保存方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile pic, Daily daily) {
        JSONResult result = new JSONResult();

        if (pic != null && pic.getSize() > 0) {
            String imagePath = UploadUtil.upload(pic, UploadUtil.IMG_PATH + "upload");
            daily.setCoverUrl(imagePath);
        }

        try {
            dailyService.saveOrUpdate(daily);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }
}
